package com.dfbz.dao;

import com.dfbz.dao.basedao.BaseDao;
import com.dfbz.model.Article;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class ArticleDao extends BaseDao<Article> {

    private JdbcTemplate jdbcTemplate = getJdbcTemplate();

    public ArticleDao(Class<Article> articleClass) {
        super(articleClass);
    }

    public Integer findAllArticleNum(String title){
        Integer integer;

        if (title==null||title==""){
            String sql="select count(*) from article";
            integer = jdbcTemplate.queryForObject(sql, Integer.class);

        }else {
            title="%"+title+"%";
            String sql="select count(*) from article where title like "+title;
            integer = jdbcTemplate.queryForObject(sql, Integer.class);
        }

        return integer;
    }

    public List<Article> findAllArticle(String title,Integer starNum,Integer pageSize){
        List<Article> articleList;
        if (null==title||""==title){
            String sql="select * from article limit ?,?";
            articleList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class), starNum, pageSize);
        }else {
            String sql="select * from article where title like ? limit ?,?";
            title="%"+title+"%";
            articleList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class), starNum, pageSize);
        }
        return articleList;
    }

    public void addBrowse(Integer articleId) {
        String sql="UPDATE article set browse_count=browse_count+1 WHERE user_id=?";
        jdbcTemplate.update(sql,articleId);
    }

    public Integer getFocusNum(int i) {
        String sql="SELECT COUNT(*) FROM favorite WHERE a_id="+i;
        return jdbcTemplate.queryForObject(sql,Integer.class);
    }

    public List<Map<String, Object>> findMyFocusUserFocusArticle(int i1, Long id) {
        String sql="SELECT us.real_name\n" +
                          "FROM favorite fa \n" +
                          "LEFT JOIN user us on fa.u_id=us.id\n" +
                          "WHERE fa.a_id=? and fa.u_id in\n" +
                          "(SELECT user_focus_id\n" +
                          "FROM userfocus\n" +
                          "WHERE user_id=?)";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, i1, id);
        return maps;
    }

    public Integer getAllMyFocusArticleNum(Long id) {
        String sql="SELECT count(*)\n" +
                          "FROM favorite fa\n" +
                          "LEFT JOIN article ar on fa.a_id=ar.user_id\n" +
                          "WHERE fa.u_id="+id;
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        return integer;
    }

    public List<Map<String, Object>> getMyFocusArticle(Long id, int i, Integer pageSize) {
        String sql="SELECT *\n" +
                          "FROM favorite fa\n" +
                          "LEFT JOIN article ar on fa.a_id=ar.user_id\n" +
                          "WHERE fa.u_id=? limit ?,?";

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, id, i, pageSize);
        return maps;

    }
}
